﻿using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace EditorFrameWork
{
    [CustomEditorWindow(3)] 
    public class TypeExExample : EditorWindow
    {
        public class DescriptionBase
        {
            public virtual string Desc { get; set; }
        }

        public class MyDescriptionA : DescriptionBase
        {
            public override string Desc { get; set; } = "描述A";
        }

        [MyDescription("TypeB")]
        public class MyDescriptionB : DescriptionBase
        {
            public override string Desc { get; set; } = "描述B";
        }

        public class MyDescriptionAttribute : Attribute
        {
            public string Type;

            public MyDescriptionAttribute(string type = default)
            {
                Type = type;
            }
        }

        private IEnumerable<Type> mDescTypes;
        private IEnumerable<Type> mDescTypesWithAttribute;


        private void OnEnable()
        {
            mDescTypes = typeof(DescriptionBase).GetSubTypesInAssemblies();
            mDescTypesWithAttribute = typeof(DescriptionBase).GetSubTypesWithClassAttributeInAssemblies<MyDescriptionAttribute>();
        }

        private void OnGUI()
        {
            GUILayout.Label("With Type");
            foreach (var mDescType in mDescTypes)
            {
                GUILayout.BeginHorizontal("box");
                {
                    GUILayout.Label(mDescType.Name);
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.Label("With Attribute");
            foreach (var mDescTypeWithAttribute in mDescTypesWithAttribute)
            {
                GUILayout.BeginHorizontal("box");
                {
                    GUILayout.Label(mDescTypeWithAttribute.Name);
                    GUILayout.Label(mDescTypeWithAttribute.GetCustomAttribute<MyDescriptionAttribute>().Type);
                }
                GUILayout.EndHorizontal();
            }
        }
    }
}